package com.jyy.prefabricated.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.dto.StudentExerciseDto;
import com.jyy.prefabricated.dto.StudentSignUpApplyDto;
import com.jyy.prefabricated.entity.Student;
import com.jyy.prefabricated.entity.StudentExercise;
import com.jyy.prefabricated.service.StudentExerciseService;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.ExamResultVo;
import com.jyy.prefabricated.vo.StudentExercisePaperVo;
import com.jyy.prefabricated.vo.StudentExerciseVo;
import com.jyy.prefabricated.vo.StudentSignUpApplyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 学员练习
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class StudentExerciseController {

    @Autowired
    private StudentExerciseService studentExerciseService;

    @Autowired
    private UserUtils userUtils;

    /**
     * 开始练习
     * @param dto
     * @return
     */
    @PostMapping("/student-exercise")
    public AjaxResult insert(@Valid @RequestBody StudentExerciseDto dto){
        log.info("Rest request to insert studentExercise:{}",dto);
        Student student = userUtils.getCurrentStudent();
        dto.setStudentId(student.getId());
        Integer id = studentExerciseService.insert(dto);
        StudentExercise result = studentExerciseService.selectById(id);
        return AjaxResult.success(result);
    }


    /**
     * 暂停练习
     * @param studentExercise
     * @return
     */
    @PutMapping("/student-exercise")
    public AjaxResult update(@Valid @RequestBody StudentExercise studentExercise){
        log.info("Rest request to update studentExercise:{}",studentExercise);
        studentExerciseService.update(studentExercise);
        StudentExercise result = studentExerciseService.selectById(studentExercise.getId());
        return AjaxResult.success(result);
    }


    /**
     * 查询当前学生最近的练习信息
     * @return
     */
    @GetMapping("/student-exercise")
    public AjaxResult selectCurrentExam(Pageable pageable){
        log.info("Rest request to find current studentExercise");
        Student student = userUtils.getCurrentStudent();
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<StudentExerciseVo> list = studentExerciseService.selectExerciseVoByStudentId(student.getId());
        PageInfo<StudentExerciseVo> result = new PageInfo<>(list);
        return AjaxResult.success(result);
    }

    /**
     * 查询我的练习列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/student-exercise-my")
    public AjaxResult selectMyExercise(StudentExerciseDto dto) {
        Student student = userUtils.getCurrentStudent();
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        dto.setStudentId(student.getId());
        List<StudentExerciseVo> list = studentExerciseService.selectExerciseVoByStudentIdAndDto(dto);
        PageInfo<StudentExerciseVo> result = new PageInfo<>(list);
        return AjaxResult.success(result);
    }

    /**
     * 查询我的错题练习列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/student-error-exercise")
    public AjaxResult selectByMyErrorExercise(StudentExerciseDto dto) {
        Student student = userUtils.getCurrentStudent();
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        dto.setStudentId(student.getId());
        List<StudentExerciseVo> list = studentExerciseService.selectExerciseVoByStudentIdAndParam(dto);
        PageInfo<StudentExerciseVo> result = new PageInfo<>(list);
        return AjaxResult.success(result);
    }

    /**
     * 交卷
     * @param studentExerciseId
     * @return
     */
    @PutMapping("/student-exercise/{studentExerciseId}")
    public AjaxResult finish(@PathVariable Integer studentExerciseId){
        log.info("Rest request to delete studentExercise by id:{}",studentExerciseId);
        ExamResultVo result = studentExerciseService.finishExercise(studentExerciseId);
        return AjaxResult.success(result);
    }

}
